//
//  SEProfileViewController.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/12.
//

import UIKit

class SEProfileViewController: SEBaseViewController {

    @IBOutlet weak var tableView: UITableView! {
        didSet {
            tableView.tableHeaderView = tableHeaderView
            tableView.tableFooterView = tableFooterView
            tableView.se_header = SERefreshHeader {
                self.requestPatientList()
            }
            tableView.rowHeight = UITableView.automaticDimension
            tableView.register(UINib(nibName: SEProfileOrderCell.className, bundle: nil), forCellReuseIdentifier: SEProfileOrderCell.reuseId)
            tableView.register(UINib(nibName: SEProfileRecordCell.className, bundle: nil), forCellReuseIdentifier: SEProfileRecordCell.reuseId)
            tableView.register(UINib(nibName: SEProfileOtherCell.className, bundle: nil), forCellReuseIdentifier: SEProfileOtherCell.reuseId)
        }
    }

    var viewModel = SEProfileViewModel()

    override func viewDidLoad() {
        super.viewDidLoad()
        requestPatientList()
        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tableHeaderView.updateView(viewModel: viewModel)
        tableView.reloadData()
    }

    override func setupNavBar() {
        super.setupNavBar()
        navigationController?.setNavigationBarHidden(true, animated: true)
//        navigationItem.titleView = UIView()
//        navigationController?.navBarStyle(.theme(color: UIColor(hex: "#45C6A8")), isTranslucent: false)
    }

    override func setupUI() {
        viewModel.updateData = { status in
            self.tableView.se_header?.endRefreshing()
            switch status {
            case .success:
                self.tableView.reloadData()
            case .auth(let msg):
                self.presentLoginViewController(selector: #selector(self.requestPatientList)) {
                    SEHUD.showError(message: msg)
                }
            case .failure(let msg):
                SEHUD.showError(message: msg)
            }
        }
    }

    lazy var tableHeaderView: SEProfileHeaderView = {
        let view = SEProfileHeaderView()
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 165)
        view.delegate = self
        return view
    }()

    lazy var tableFooterView: UIView = {
        let view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.screenWidth, height: 50)
        let label = UILabel()
        label.frame = CGRect(x: 15, y: 20, width: UIScreen.screenWidth - 30, height: 20)
        label.textAlignment = .center
        label.text = "客户服务热线：4001877888"
        label.font = UIFont.regular(14)
        label.textColor = Asset.textGrayLightest.color
        view.addSubview(label)
        return view
    }()

    private func pushToSettingViewController() {
        let vc = SESettingViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

}

// MARK: - UITableViewDataSource, UITableViewDelegate

extension SEProfileViewController: UITableViewDataSource, UITableViewDelegate {

    // MARK: - UITableViewDataSource

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.row == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEProfileOrderCell.reuseId, for: indexPath) as! SEProfileOrderCell
            cell.delegate = self
            return cell
        }
        if indexPath.row == 1 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEProfileRecordCell.reuseId, for: indexPath) as! SEProfileRecordCell
            cell.delegate = self
            cell.updateCell(viewModel: viewModel)
            return cell
        }
        if indexPath.row == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: SEProfileOtherCell.reuseId, for: indexPath) as! SEProfileOtherCell
            cell.delegate = self
            return cell
        }
        return UITableViewCell()
    }
}

// MARK: - SEProfileHeaderViewDelegate

extension SEProfileViewController: SEProfileHeaderViewDelegate {
    func profileHeaderViewTapAction(type: SEProfileHeaderViewActionType) {
        switch type {
        case .setting:
            pushToSettingViewController()
        case .name:
            presentLoginViewController(selector: #selector(requestPatientList))
        case .avator:
            print("avator")
        }
    }

}

// MARK: - SEProfileOrderCellDelegate

extension SEProfileViewController: SEProfileOrderCellDelegate {
    func profileOrderCellTapAction(type: SEProfileOrderCellActionType) {
        switch type {
        case .hospitalize:
            let vc = SEHospitalizeOrderListViewController(with: .profile)
            navigationController?.pushViewController(vc, animated: true)
        case .examination: pushToOrderExamineVC()
        case .prescription:
            let vc = SEPrescriptionOrderListViewController()
            navigationController?.pushViewController(vc, animated: true)
        }
    }
}

// MARK: - SEProfileOtherCellDelegate

extension SEProfileViewController: SEProfileOtherCellDelegate {
    func profileOtherCellTapAction(type: SEProfileOtherCellTapType) {
        switch type {
        case .registration:
            let vc = SEMyRegistrationListViewController()
            navigationController?.pushViewController(vc, animated: true)
        case .attention: pushToMyAttentionVC()
        case .patient:
            let vc = SEPatientListViewController(with: .list)
            navigationController?.pushViewController(vc, animated: true)
        case .help:
            SEHUD.show(message: "帮助反馈")
        }
    }
}

// MARK: - SEProfileRecordCellDelagete

extension SEProfileViewController: SEProfileRecordCellDelagete {
    func profileRecordCell(viewModel: SEPatientDetailViewModel, cellType: SEProfileRecordCvCellType) {
        switch cellType {
        case .add:
            pushToPatientAddVc()
        case .login:
            presentLoginViewController(selector: #selector(requestPatientList))
        case .info:
            self.viewModel.selectedPatientViewModel = viewModel
            pushToPatientFileVc()
        }
    }
}

// MARK: - push action event

extension SEProfileViewController {
    /// 跳转 检查 订单
    private func pushToOrderExamineVC() {
        let vc = SEOrderExamineViewController(with: .profile)
        vc.title = "检查订单"
        navigationController?.pushViewController(vc, animated: true)
    }
    
    /// 跳转 档案 详情
    private func pushToPatientFileVc() {
        let vc = SEPatientFileViewController(with: viewModel.selectedPatientViewModel)
        navigationController?.pushViewController(vc, animated: true)
    }

    /// 跳转 新增 就诊人
    private func pushToPatientAddVc() {
        let vc = SEPatientDetailViewController(with: viewModel.selectedPatientViewModel, editType: .add)
        vc.submitSuccessHandler = {
            self.requestPatientList()
        }
        navigationController?.pushViewController(vc, animated: true)
    }
    
    // MARK: - Other
    
    /// 跳转 我的关注
    private func pushToMyAttentionVC() {
        let vc = SEFollowedViewController()
        vc.title = "我的关注"
        navigationController?.pushViewController(vc, animated: true)
    }
}

extension SEProfileViewController {
    @objc private func requestPatientList() {
        viewModel.requestPatientList()
    }
}


